#!/bin/sh -eu
#
# Copyright 2008 Mattias Norrby
#
# This file is part of Test Dept..
#
# Test Dept. is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Test Dept. is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Test Dept..  If not, see <http://www.gnu.org/licenses/>.

SUT="$1"
DEST="$2"

SYMBOLS="/tmp/test_dept_proxies$$"

create_file() {
  FILE="$1"
  if echo "# symbols to replace to make stubbable code" >${FILE}; then
    echo "Alles gut" >/dev/null
  else
    echo "$0: Could not create ${FILE}" >&2
    exit 2
  fi
}

construct_symbol_replacement_table() {
  nm -p $SUT | grep " U " | awk '{print $NF " " $NF "_test_dept_proxy" }'
}

remove_file() {
  FILE="$1"
  rm -f $FILE
}

create_file $SYMBOLS
construct_symbol_replacement_table >$SYMBOLS
objcopy --redefine-syms ${SYMBOLS} ${SUT} ${DEST}
rm -f $SYMBOLS

